package top.fkxuexi.blogapi.modules.auth.controller;


import com.sun.org.apache.xpath.internal.operations.Mod;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import top.fkxuexi.blogapi.core.config.shiro.ShiroToken;
import top.fkxuexi.blogapi.core.utils.StringUtils;
import top.fkxuexi.blogapi.core.utils.common.Code;
import top.fkxuexi.blogapi.core.utils.common.Result;

@Controller
public class SysUserLoginController {


    private static String prefix = "framework/";


    @RequestMapping(value = "/login")
    public ModelAndView login() {
        ModelAndView mv = new ModelAndView("login");
        return mv;
    }


    @RequestMapping(value = "/login/action", method = RequestMethod.POST)
    @ResponseBody
    public Result doLogin(String loginName, String pwd) {

        if (StringUtils.isEmpty(loginName, pwd)) {
            return new Result(Code.PARAM_ERROR, false);
        }
        ShiroToken token = new ShiroToken(loginName, pwd, ShiroToken.ShiroType.ADMIN);
        Subject subject = SecurityUtils.getSubject();
        if (!subject.isAuthenticated()) {
            try {
                subject.login(token);
            } catch (Exception e) {
                return new Result(Code.FAILD, false);
            }
        }
        return new Result(Code.SUCCESS, true);
    }


    @RequestMapping(value = "/index/page")
    public ModelAndView index() {
        return new ModelAndView(prefix + "home");
    }

    @RequestMapping(value = "/pjax/test")
    public ModelAndView pjax(){
        return new ModelAndView(prefix + "default");
    }


}
